package ngames.com.sushitv.adapter;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.action.SearchPromptAction;
import ngames.com.sushitv.base.BaseListAdapter;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.PromptUser;

/**
 * Created by Fearless on 2016/6/26.
 */
public class SearchPromptAdapter extends BaseListAdapter<String> {
    private List<String> historyStr;
    private LayoutInflater minflater;
    private Context context;
    private List<PromptUser> promptUsers;
    private int flag;
    private SearchPromptAction searchPrompt;
    public SearchPromptAdapter(Context context, ArrayList<String> historyStr, List<PromptUser> promptUsers, int i,SearchPromptAction searchPromptAction) {
        super(context, historyStr);
        this.historyStr = historyStr;
        this.promptUsers = promptUsers;
        this.context = context;
        flag = i;
        minflater = LayoutInflater.from(context);
        searchPrompt=searchPromptAction;
    }


    @Override
    public HistoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = minflater.inflate(R.layout.view_textview_prompt, null);
        HistoryHolder viewHolder = new HistoryHolder(convertView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       /* for(int i=0;i<position;++i) {
            holder.historyText.setText(historyStr.get(i));
        }*/
        HistoryHolder historyHolder = (HistoryHolder) holder;
        historyHolder.setData(position);
    }


    @Override
    public int getItemCount() {
        if (flag == 1) {
            return historyStr.size();
        } else {
            return promptUsers.size();
        }
    }

    class HistoryHolder extends RecyclerView.ViewHolder {
        private TextView historyText;

        public HistoryHolder(View itemView) {
            super(itemView);
            historyText = (TextView) itemView.findViewById(R.id.text_search_prompt);
        }

        public void setData(int position) {
            String key="";
            switch (flag) {
                case 1:
                    String prompt = getItem(position);
                    historyText.setText(prompt);
                    key=prompt;
                    break;
                case 2:
                    PromptUser promptUser = promptUsers.get(position);
                    String str = promptUser.getName();
                    historyText.setText(str);
                    key=str;
                    break;
            }
            final String finalKey = key;
            historyText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppApplication.getChangFragment().ChangeFragment(3);
                    AppApplication.setSearchKeyWord(finalKey);
                    AppApplication.setSearchRequestType(flag+"");
                    //searchPrompt.searchResultData(finalKey,flag);

                }
            });

        }
    }
}
